﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HDInsightStudio
{
    /// <summary>
    /// HdfsStudioView.xaml の相互作用ロジック
    /// </summary>
    public partial class HdfsStudioView : UserControl
    {
        public HdfsStudioView()
        {
            InitializeComponent();
        }

        private void TreeViewItem_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sender is TreeViewItem)
            {
                var item = sender as TreeViewItem;
                item.IsSelected = true;
                e.Handled = true;
            }
        }

        private HdfsStudio GetViewModel()
        {
            return (HdfsStudio)this.DataContext;
        }

        private void UploadClick(object sender, RoutedEventArgs e)
        {
            GetViewModel().UploadCommand.Execute(null);   
        }

        private void DeleteFolderClick(object sender, RoutedEventArgs e)
        {
            GetViewModel().DeleteFolderCommand.Execute(null);   

        }

        private void CreateFolderClick(object sender, RoutedEventArgs e)
        {
            GetViewModel().Input = "New Folder";
            GetViewModel().CreateDirectoryCommand.Execute(null);   

        }

        private void DeleteClick(object sender, RoutedEventArgs e)
        {
            var target = GetFileItem(sender);
            GetViewModel().DeleteItemCommand.Execute(target);

        }

        private void RefreshClick(object sender, RoutedEventArgs e)
        {
            GetViewModel().UpdateCommand.Execute(null);

        }

        private void DownloadClick(object sender, RoutedEventArgs e)
        {
            var target = GetFileItem(sender);
            GetViewModel().DownloadCommand.Execute(target);

        }

        private FileItem GetFileItem(object sender)
        {
            var menuItem = (MenuItem)sender;
            var contextMenu = (ContextMenu)menuItem.Parent;
            var item = (DataGrid)contextMenu.PlacementTarget;
            var target = (FileItem)item.SelectedCells[0].Item;
            return target;
        }

        private void Files_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var target = (FileItem)Files.SelectedCells[0].Item;
            GetViewModel().DownloadCommand.Execute(target);
        }

    }
}
